﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameHelper
{
    public partial class MainBanner : Form
    {
        /// <summary>
        /// 当前是否可以攻击
        /// </summary>
        bool couleAttack = true;
        /// <summary>
        /// 攻击线程
        /// </summary>
        System.Threading.Thread attackThread = null;
        /// <summary>
        /// 一个怪物的攻击次数
        /// </summary>
        int attackCount = 0;
        /// <summary>
        /// 总计的杀怪数量
        /// </summary>
        int killAllCount = 0;
        /// <summary>
        /// 程序运行时间
        /// </summary>
        int runTime = 0;



        public MainBanner()
        {
            InitializeComponent();
            List<WindowAPI.Medicine> medics = WindowAPI.Medicine.GetAllMedicine();
            if (medics.Count > 0)
            {
                InitBloodConfig(medics);
            }

            List<WindowAPI.Skill> skills = WindowAPI.Skill.GetAllSkill();
            if (skills.Count > 0)
            {
                InitSkillConfig(skills);
            }


        }

        private void label4_Click(object sender, EventArgs e)
        {
            WindowAPI.MouseHook hook = new WindowAPI.MouseHook();
            WindowAPI.MouseHook.nowHockPart = 1;
            hook.Start();
        }
        /// <summary>
        /// 开始挂机按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(this.textBox11.Text, out i);
            int.TryParse(this.textBox12.Text, out i);
            int.TryParse(this.textBox14.Text, out i);
            int.TryParse(this.textBox16.Text, out i);
            int.TryParse(this.textBox18.Text, out i);

            GameHelper.InsertBloodConfig(this.textBox6.Text, Convert.ToInt32(this.textBox8.Text), this.textBox5.Text, Convert.ToInt32(this.textBox7.Text), this.textBox3.Text, Convert.ToInt32(this.textBox1.Text), this.textBox4.Text, Convert.ToInt32(this.textBox2.Text));
            GameHelper.InsertSkillConfig(this.textBox10.Text, i, this.textBox13.Text, i, this.textBox15.Text, i, this.textBox17.Text, i, this.textBox19.Text, i);



            attackThread = new System.Threading.Thread(new System.Threading.ThreadStart(StartAttacking));
            attackThread.Start();
        }
        public void InitBloodConfig(List<WindowAPI.Medicine> medics)
        {
            this.textBox8.Text = medics[3].CoolingTime.ToString();
            this.textBox6.Text = medics[3].Key;

            this.textBox7.Text = medics[2].CoolingTime.ToString();
            this.textBox5.Text = medics[2].Key;

            this.textBox1.Text = medics[1].CoolingTime.ToString();
            this.textBox3.Text = medics[1].Key;

            this.textBox2.Text = medics[0].CoolingTime.ToString();
            this.textBox4.Text = medics[0].Key;
        }

        public void InitSkillConfig(List<WindowAPI.Skill> skills)
        {
            skills.OrderByDescending(a => a.Id);
            if (skills.Count > 0)
            {
                this.textBox11.Text = skills[0].CoolingTime.ToString();
                this.textBox10.Text = skills[0].Key;
            }
            if (skills.Count > 1)
            {
                this.textBox12.Text = skills[1].CoolingTime.ToString();
                this.textBox13.Text = skills[1].Key;
            }
            if (skills.Count > 2)
            {
                this.textBox14.Text = skills[2].CoolingTime.ToString();
                this.textBox15.Text = skills[2].Key;
            }
            if (skills.Count > 3)
            {
                this.textBox16.Text = skills[3].CoolingTime.ToString();
                this.textBox17.Text = skills[3].Key;
            }
            if (skills.Count > 4)
            {
                this.textBox18.Text = skills[4].CoolingTime.ToString();
                this.textBox19.Text = skills[4].Key;
            }
        }

        /// <summary>
        /// 这只是一个测试的方法，在以后一定会加强这个东西
        /// </summary>
        private void StartAttacking()
        {
            System.Threading.Thread.Sleep(5000);
            while (couleAttack)
            {
                System.Drawing.Bitmap img = WindowAPI.ApiHelper.GetScreenMap();
                GameHelper gh = new GameHelper();
                if (GameHelper.HasGuaiToAttack(img))//直接点击攻击
                {
                    GameHelper.Attack(ref attackCount, runTime);
                }
                else//锁定怪物
                {
                    if (GameHelper.BloodIsMany(img))//如果剩余血量足够
                    {
                        GameHelper.ChangeToAttack(ref attackCount);
                        killAllCount++;//又杀死一个怪物

                    }
                    else
                    {
                        //休息
                    }
                }
                img = null;
                runTime++;
                System.Threading.Thread.Sleep(1000);
            }
        }

        private void SaveMessage()
        {
            string message = this.textBox35.Text;
            int coolingTime = 30;
            if (!string.IsNullOrEmpty(message))
            {
                WindowAPI.Message mess = new WindowAPI.Message();
                mess.Content = message;
                int.TryParse(this.textBox36.Text, out coolingTime);
                mess.CoolingTime = coolingTime;
                mess.Insert();
            }
        }




        /// <summary>
        /// 停止挂机按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            couleAttack = false;
            attackThread.Abort();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            WindowAPI.MouseHook hook = new WindowAPI.MouseHook();
            WindowAPI.MouseHook.nowHockPart = 2;
            hook.Start();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            WindowAPI.MouseHook hook = new WindowAPI.MouseHook();
            WindowAPI.MouseHook.nowHockPart = 3;
            hook.Start();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            WindowAPI.MouseHook hook = new WindowAPI.MouseHook();
            WindowAPI.MouseHook.nowHockPart = 4;
            hook.Start();
        }

        /// <summary>
        /// 记录怪物攻击点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            WindowAPI.MouseHook hook = new WindowAPI.MouseHook();
            WindowAPI.MouseHook.nowHockPart = -2;
            hook.Start();
        }

    }
}
